import 'package:eyepetizer_example/model/common_item.dart';
import 'package:eyepetizer_example/page/video/video_detail_page.dart';
import 'package:eyepetizer_example/widget/go_navigation.dart';
import 'package:flutter/material.dart';

import '../../config/string.dart';
import '../../utils/cache_image.dart';
import '../../utils/date_util.dart';

class FollowItemWidget extends StatelessWidget {

  final ItemList item;
  const FollowItemWidget({Key? key,
    required this.item
  }) : super(key: key);

  @override
  Widget build(BuildContext context) {

    return Container(
      decoration: const BoxDecoration(color: Colors.white),
      child: Column(
        children: [
          //视频作者介绍
          _videoAuthor(),
          //作者作品
          SizedBox(
            height: 230,
            child: ListView.builder(
              itemCount: item.data?.itemList?.length,
              scrollDirection: Axis.horizontal,
              itemBuilder: (context, index){
                var lastIndex = item.data?.itemList?.length??0;
                var itemData = item.data?.itemList?[index];
                if(itemData == null) {
                  return Container();
                }
                return _inkWell(
                  item: itemData,
                  last: index == lastIndex - 1,
                  context: context
                );
              },
            ),
          )
        ],
      ),
    );
  }


  Widget _inkWell({required ItemList item, required bool last, required context}) {
    return InkWell(
      onTap: (){
        if(item.data == null ) {
          return;
        }
        GoNavigator.toPage(context, VideoDetailPage(item.data!));
      },
      child: Container(
        padding: EdgeInsets.only(left: 15, right: last ? 15 : 0),
        child: Column(
          crossAxisAlignment: CrossAxisAlignment.start,
          children: [
            // 视频图片
            _videoImage(item),
            _videoName(item),
            _videoTime(item),
          ],
        ),
      ),
    );
  }


  /// 视频图片
  Widget _videoImage(ItemList item) {
    return Stack(
      children: [
        ClipRRect(
          borderRadius: BorderRadius.circular(4),
          child: Hero(
            tag: '${item.data?.id}${item.data?.time}',
            child: cacheImage(
              item.data?.cover?.feed??"",
              width: 300,
              height: 180,
            ),
          ),
        ),

        Positioned(
          right: 8,
          top: 8,
          child: ClipRRect(
            borderRadius: BorderRadius.circular(5),
            child: Container(
              padding: const EdgeInsets.all(6),
              decoration: const BoxDecoration(color: Colors.white54),
              alignment: AlignmentDirectional.center,
              child: Text(
                item.data?.category??"",
                style: const TextStyle(
                  fontSize: 13,
                  color: Colors.black,
                  fontWeight: FontWeight.bold
                ),
              ),
            ),
          ),
        )
      ],
    );
  }

  /// 视频名称
  Widget _videoName(ItemList item) {
    return Container(
      width: 300,
      padding: const EdgeInsets.only(top: 3, bottom: 3),
      child: Text(
        item.data?.title??"",
        style: const TextStyle(
          fontSize: 14,
          color: Colors.black,
          fontWeight: FontWeight.bold
        ),
        maxLines: 1,
        overflow: TextOverflow.ellipsis,
      ),
    );
  }
  /// 视频上线时间
  Widget _videoTime(ItemList item) {
    return Text(
      formatDateMsByYMDHM(item.data?.author?.latestReleaseTime??0),
      style: const TextStyle(fontSize: 12, color: Colors.black26),
    );
  }

  /// 视频作者介绍
  Widget _videoAuthor() {
    return Container(
      padding: const EdgeInsets.fromLTRB(15, 10, 15, 10),
      child: Row(
        children: [
          ClipOval(
            child: cacheImage(item.data?.header?.icon??"", width: 40, height: 40),
          ),
          Expanded(
            flex: 1,
            child: Container(
              padding: const EdgeInsets.only(left: 10, right: 10),
              child: Column(
                crossAxisAlignment: CrossAxisAlignment.start,
                children: [
                  Text(
                    item.data?.header?.title??"",
                    style: const TextStyle(color: Colors.black, fontSize: 14),
                  ),
                  Padding(
                    padding: EdgeInsets.only(top: 3),
                    child: Text(
                      item.data?.header?.description??"",
                      style: const TextStyle(
                        color: Colors.black26,
                        fontSize: 12
                      ),
                      maxLines: 1,
                      overflow: TextOverflow.ellipsis,
                    ),
                  )
                ],
              ),
            ),
          ),

          ClipRRect(
            borderRadius: BorderRadius.circular(5),
            child: Container(
              padding: const EdgeInsets.all(5),
              decoration: const BoxDecoration(color: Color(0xFFF4F4F4)),
              child: const Text(
                AppString.add_follow,
                style: TextStyle(fontSize: 12, color: Colors.grey),
              ),
            ),
          )
        ],
      ),
    );
  }
}

